package com.yixun.qfbao.api.service.impl;

import com.yixun.qfbao.api.service.PremisesPermissionService;
import com.yixun.qfbao.constant.RedisKeyConstant;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.model.enums.MechanismAccountTypeEnum;
import com.yixun.qfbao.service.PartnerInfoService;
import com.yixun.qfbao.util.RedisUtil;
import com.yixun.qfbao.xf.model.CaseMember;
import com.yixun.qfbao.xf.model.Premises;
import com.yixun.qfbao.xf.model.dto.CaseMemberDto;
import com.yixun.qfbao.xf.model.dto.PremisesDto;
import com.yixun.qfbao.xf.service.CaseMemberService;
import com.yixun.qfbao.xf.service.PremisesService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class PremisesPermissionServiceImpl implements PremisesPermissionService {

    @Autowired
    private PartnerInfoService partnerInfoService;
    @Autowired
    private PremisesService premisesService;
    @Autowired
    private CaseMemberService caseMemberService;

    /**
     * 获取用户能看到的所有楼盘
     * @param partnerId
     * @return
     */
    @Override
    public List<Long> getPartnerPremisesPermission(Long partnerId) {
        List<Long> premisesIds = new ArrayList<>();
        PartnerInfo partnerInfo = partnerInfoService.selectById(partnerId);
        String devUserPremisesPermission = RedisKeyConstant.Permission.DEV_USER_PREMISES_PERMISSION + partnerId;
        if (partnerInfo.getAccountType().equals(MechanismAccountTypeEnum.mechanism.getNo())) {
            // 管理员: 获取开发商所有楼盘
            PremisesDto pDto = new PremisesDto();
            pDto.setCompanyId(partnerInfo.getHigherid());
            List<Premises> premisesList = premisesService.selectIdListByDto(pDto);
            premisesIds = premisesList.stream().map(Premises::getId).collect(Collectors.toList());
            RedisUtil.set(devUserPremisesPermission, premisesIds);
            RedisUtil.expire(devUserPremisesPermission, 10, TimeUnit.DAYS);
            return premisesIds;
        }

        if(RedisUtil.hasKey(devUserPremisesPermission)){
            Object object = RedisUtil.get(devUserPremisesPermission);
            if(Objects.nonNull(object)){
                premisesIds = (List<Long>)object;
                return premisesIds;
            }
        }

        // 个人驻守ID
        CaseMemberDto caseMemberDto = new CaseMemberDto();
        caseMemberDto.setPartnerId(partnerInfo.getId());
        caseMemberDto.setCompanyId(partnerInfo.getHigherid());
        List<CaseMember> cmList = caseMemberService.selectListByDto(caseMemberDto);
        premisesIds = cmList.stream().map(CaseMember::getPremisesId).collect(Collectors.toList());

        //当前登录人是否为楼盘项目总监或项目经理
        PremisesDto premisesDto = new PremisesDto();
        premisesDto.setMobileTemp(partnerInfo.getPhone());
        premisesDto.setCompanyId(partnerInfo.getHigherid());
        List<Premises> premisesList = premisesService.selectIdListByMobileTemp(premisesDto);
        if (CollectionUtils.isNotEmpty(premisesList)) {
            for (Premises premises : premisesList) {
                premisesIds.add(premises.getId());
            }
        }
        // 此逻辑放到具体业务中处理,目前只有获取报备列表需要特殊处理
        // 当楼盘为空时,还是有机会能看到部分数据(绑定置业顾问的报备)
        //if(CollectionUtils.isEmpty(premisesIds)){
            // 当只有置业顾问时,获取全部楼盘.此时用户将无楼盘过滤条件
            // 在此处添加一个 -1, 让条件只进入置业顾问的 or 条件.
            // 故只能查出置业顾问相关的报备.此处不加会查出系统所有报备
        //    premisesIds.add(-1L);
        //}
        RedisUtil.set(devUserPremisesPermission, premisesIds);
        RedisUtil.expire(devUserPremisesPermission, 10, TimeUnit.DAYS);
        return premisesIds;
    }

}
